package com.example.demo.repository;

import com.example.demo.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Date;

/**
 * 用户管理Repository
 * @Author zmf
 * @Date 2018/12/12  17:31
 */
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
    @Modifying
    @Query("update User set status =:status,passwordUpdateDate =:nowDate where userId =:userId")
    void updateStatusAndPasswordUpdateDate(@Param(value = "status") Integer status,
                                           @Param(value = "nowDate") Date nowDate,
                                           @Param(value = "userId") Long userId);

    @Modifying
    @Query("update User set status =:status,passwordUpdateDate=:date where userId =:userId")
    void updateStatus(@Param(value = "status") Integer status,
                      @Param(value = "date") Date date,
                      @Param(value = "userId") Long userId);


    @Modifying
    @Query("update User set status =:status where userId =:userId")
    void updateStatus(@Param(value = "status") Integer status,
                      @Param(value = "userId") Long userId);

    User getUserByUserId(Long userId);

    @Modifying
    @Query("update User set password =:password,updateTime =:date,passwordUpdateDate =:date where userId =:userId")
    void resetPwd(@Param(value = "password") String password,
                  @Param(value = "date") Date date,
                  @Param(value = "userId") Long userId);

    @Modifying
    @Query("update User set passwordUpdateDate =:date where userId =:userId")
    void updatePasswordDateTime(
                  @Param(value = "date") Date date,
                  @Param(value = "userId") Long userId);
}
